//
//  BiCarSaleNetEngine.m
//  TrunkIndustryManageSystem
//
//  Created by 李璐西 on 15-4-6.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//

#import "BiCarSaleNetEngine.h"
#import "BiNetEngine.h"
#import "ASIFormDataRequest.h"
#import "BiCarSaleParser.h"
#import "ASINetworkQueue.h"

@implementation BiCarSaleNetEngine
SINGLE_INSTANCE_IMPLEMENTION(BiCarSaleNetEngine)

/**
 *   请求整车销售列表， 下拉刷新请求更多，dict为传入参数字典
 */
- (void)requestCarSaleCheckList:delegate paraDict:(NSDictionary *)dict
{
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarXingxiao/getList",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_CAR_SALE_CHECK_LIST]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_CAR_SALE_CHECK_LIST];
    
}


/**
 *   //行销申请／合同交接列表,dict为传入参数字典
 */
- (void)requestCarSaleContracAssoList:(id)delegate paraDict:(NSDictionary *)patadict
{
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarSale/getList",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_CAR_SALE_CONTRACT_LIST]];
    
    [self createPostRequest:url paramDic:patadict tag:NET_GET_CAR_SALE_CONTRACT_LIST];
    
}

/**
 *   行销申请保存，dict为传入参数字典
 */
- (void)requestSaleApplySave:(id)delegate paraDict:(NSDictionary *)dict{

    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarXingxiao/saveXingxiao",SERVICE_URL];
   // NSString *url = [NSString stringWithFormat:@"http://192.168.0.104/erp/mobile.php/CarXingxiao/saveXingxiao"];

    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_XINGXIAO_APPLY_LIST]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_XINGXIAO_APPLY_LIST];
    
    
}


/**
 *   行销申请审核，dict为传入参数字典
 */
- (void)requestSaleApplyCheck:(id)delegate paraDict:(NSDictionary *)dict{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarXingxiao/modify",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_XINGXIAO_SALEAPPLY_CHECK]];
    
    [self createPutRequest:url paramDic:dict tag:NET_GET_XINGXIAO_SALEAPPLY_CHECK];
    
    
}

/**
 *   //行销执行－>开始行销，dict为传入参数字典
 */
- (void)requestSaleStart:(id)delegate paraDict:(NSDictionary *)dict{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarXingxiao/startXingxiao",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_XINGXIAO_START_SALE]];
    
    [self createPutRequest:url paramDic:dict tag:NET_GET_XINGXIAO_START_SALE];
    
}

/**
 *   //行销执行－>结束行销，Arr为传入参数
 */
- (void)requestSaleEnd:(id)delegate paraDic:(NSDictionary *)paradic{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarXingxiao/uploadGps",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_XINGXIAO_END_SALE]];
    
    [self createPostRequest:url paramDic:paradic tag:NET_GET_XINGXIAO_END_SALE];
    
}

/**
 *   //行销执行－>获取客户信息列表，dict为传入参数字典
 */
- (void)requestSaleGetCustomerList:(id)delegate paraDict:(NSDictionary *)dict{

    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarXingxiao/getCustomerList",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_XINGXIAO_GETCUSTOMER_LIST]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_XINGXIAO_GETCUSTOMER_LIST];
    
}


/**
 *   //行销执行－>获取gps列表，dict为传入参数字典
 */
- (void)requestSaleGetGPSInfo:(id)delegate paraDict:(NSDictionary *)dict{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarXingxiao/getGpsData",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_XINGXIAO_GPS_LIST]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_XINGXIAO_GPS_LIST];
    
}


/**
 *   //行销执行－>添加客户信息，Arr为传入参数字典
 */
- (void)requestSaleAddCustomerInfo:(id)delegate paraArr:(NSArray *)Arr{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarXingxiao/saveCustomer",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_XINGXIAO_ADDCUSTOMER_INFO]];
    
    [self createPostRequest:url paramArr:Arr tag:NET_GET_XINGXIAO_ADDCUSTOMER_INFO];
}

/**
 *   //行销合同交接保存，Arr为传入参数字典
 */
- (void)requestSaleContractAssocSave:(id)delegate paraDict:(NSDictionary *)dict
{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarSale/saveSaleInfo",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_XINGXIAO_CONTRACT_SAVE]];
    
    [self createPutRequest:url paramDic:dict tag:NET_GET_XINGXIAO_CONTRACT_SAVE];
}

//行销合同交接审核
- (void)requestSaleContractAssocCheck:(id)delegate paraDict:(NSDictionary *)dict{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarSale/modify",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_XINGXIAO_CONTRACT_CHECK]];
    
    [self createPutRequest:url paramDic:dict tag:NET_GET_XINGXIAO_CONTRACT_CHECK];
}


//行销添加客户信息图片

- (void)requestSaleCuntomerImageCusT:(id)delegate withImge:(UIImage *)image paraDic:(NSDictionary *)paraDic{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarXingxiao/uploadImg",SERVICE_URL];
    
      //  NSString *url = [NSString stringWithFormat:@"http://192.168.0.103/erp/mobile.php/CarXingxiao/uploadImg?XDEBUG_SESSION_START"];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_XINGXIAO_CUSTOER_UPIMAGE1]];
    
    [self createPostImageRequest:url image:image paraDic:paraDic tag:NET_GET_XINGXIAO_CUSTOER_UPIMAGE1];

}
- (void)requestSaleCuntomerImageCusTCar:(id)delegate withImge:(UIImage *)image paraDic:(NSDictionary *)paraDic{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarXingxiao/uploadImg",SERVICE_URL];
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_XINGXIAO_CUSTOER_UPIMAGE2]];
    
    [self createPostImageRequest:url image:image paraDic:paraDic tag:NET_GET_XINGXIAO_CUSTOER_UPIMAGE2];
    
}
- (void)requestSaleCuntomerImageCusTEat:(id)delegate withImge:(UIImage *)image paraDic:(NSDictionary *)paraDic{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarXingxiao/uploadImg",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_XINGXIAO_CUSTOER_UPIMAGE3]];
    
    [self createPostImageRequest:url image:image paraDic:paraDic tag:NET_GET_XINGXIAO_CUSTOER_UPIMAGE3];
    
}

//行销添加合同信息图片
- (void)requestSaleContractImageWPJJD:(id)delegate withImge:(UIImage *)image paraDic:(NSDictionary *)paraDic{

    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarSale/uploadImg",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_XINGXIAO_CONTRACT_UPIMAGEWPJJD]];
    
    [self createPostImageRequest:url image:image paraDic:paraDic tag:NET_GET_XINGXIAO_CONTRACT_UPIMAGEWPJJD];

}

//行销添加合同信息图片
- (void)requestSaleContractImageCLJJD:(id)delegate withImge:(UIImage *)image paraDic:(NSDictionary *)paraDic{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarSale/uploadImg",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_XINGXIAO_CONTRACT_UPIMAGECLJJD]];
    
    [self createPostImageRequest:url image:image paraDic:paraDic tag:NET_GET_XINGXIAO_CONTRACT_UPIMAGECLJJD];
    
}
//行销添加合同信息图片
- (void)requestSaleContractImageRCHY:(id)delegate withImge:(UIImage *)image paraDic:(NSDictionary *)paraDic{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarSale/uploadImg",SERVICE_URL];
//NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarSale/uploadImg?XDEBUG_SESSION_START",SERVICE_URL];
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_XINGXIAO_CONTRACT_UPIMAGERCHY]];
    
    [self createPostImageRequest:url image:image paraDic:paraDic tag:NET_GET_XINGXIAO_CONTRACT_UPIMAGERCHY];
    
}
//行销删除合同信息图片
- (void)requestSaleContractImageDel:(id)delegate  paraDic:(NSDictionary *)paraDic{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarSale/deleteImg",SERVICE_URL];
    
    // NSString *url = [NSString stringWithFormat:@"http://192.168.0.107/erp/mobile.php/CarSale/deleteImg?XDEBUG_SESSION_START"];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_XINGXIAO_CONTRACT_DEL_IMAGE]];
    
    [self createPostRequest:url paramDic:paraDic tag:NET_GET_XINGXIAO_CONTRACT_DEL_IMAGE];
    
}



- (void)requestDone:(ASIHTTPRequest *)request
{
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    int httpCode = [request responseStatusCode];
    
    if (200 > httpCode || 300 <= httpCode)
    {
        NSLog(@"http error --- tag = %d, http code = %d", tag, httpCode);
        assert(0);
        return;
    }
    
    switch (tag)
    {
        case NET_GET_CAR_SALE_CHECK_LIST:
        {
            [self onGetCarSaleCheckListData:request];
        }
            break;
        case NET_GET_CAR_SALE_CONTRACT_LIST:
        {
            [self onGetCarSaleContractListData:request];
        }
            break;
        case NET_GET_XINGXIAO_APPLY_LIST:
        {
            [self onGetCarSaleApplyData:request];
        }
            break;
        case NET_GET_XINGXIAO_SALEAPPLY_CHECK:
        {
            [self onGetCarSaleApplyCheckData:request];
        }
            break;
        case NET_GET_XINGXIAO_START_SALE:
        {
            [self onGetCarSaleStartSaleData:request];
        }
            break;
        case NET_GET_XINGXIAO_END_SALE:
        {
            [self onGetCarSaleCommonkData:request];
        }
            break;
        case NET_GET_XINGXIAO_GETCUSTOMER_LIST:
        {
            [self onGetCarSaleGetCustomerListData:request];
        }
            break;
        case NET_GET_XINGXIAO_ADDCUSTOMER_INFO:
        {
            [self onGetCarSaleAddCustomerInfoData:request];
        }
            break;
        case NET_GET_XINGXIAO_GPS_LIST:
        {
            [self onGetCarSaleGPSListData:request];
        }
            break;
        case NET_GET_XINGXIAO_CONTRACT_CHECK:
        case NET_GET_XINGXIAO_CONTRACT_SAVE:
        case NET_GET_XINGXIAO_CUSTOER_UPIMAGE1:
        case NET_GET_XINGXIAO_CUSTOER_UPIMAGE2:
        case NET_GET_XINGXIAO_CUSTOER_UPIMAGE3:
        case NET_GET_XINGXIAO_CONTRACT_UPIMAGEWPJJD:
        case NET_GET_XINGXIAO_CONTRACT_UPIMAGECLJJD:
        case NET_GET_XINGXIAO_CONTRACT_UPIMAGERCHY:
        case NET_GET_XINGXIAO_CONTRACT_DEL_IMAGE:
        {
            [self onGetCarSaleCommonkData:request];
        }
            break;
        default:
            break;
    }
    
}

- (void)onGetCarSaleCheckListData:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary * dict = [BiCarSaleParser parserCarSaleCheckListEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:dict userInfo:nil];
    }
}

- (void)onGetCarSaleContractListData:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary * dict = [BiCarSaleParser parserCarSaleContractListEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:dict userInfo:nil];
    }
}

- (void)onGetCarSaleApplyData:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary * resultDic = [BiCarSaleParser parserCarSaleApplyEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:resultDic userInfo:nil];
    }
}

- (void)onGetCarSaleApplyCheckData:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary * resultDic = [BiCarSaleParser parserCommonDict:[request responseString]];
        [delegate onNetEngineResponse:tag entity:resultDic userInfo:nil];
    }
}



- (void)onGetCarSaleStartSaleData:(ASIHTTPRequest *)request{

    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary * resultDic = [BiCarSaleParser parserCommonDict:[request responseString]];
        [delegate onNetEngineResponse:tag entity:resultDic userInfo:nil];
    }
}


- (void)onGetCarSaleGetCustomerListData:(ASIHTTPRequest *)request{
    
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary * resultDic = [BiCarSaleParser parserCarSaleGetCustomerListEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:resultDic userInfo:nil];
    }
}

- (void)onGetCarSaleAddCustomerInfoData:(ASIHTTPRequest *)request{
    
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary * resultDic = [BiCarSaleParser parserCommonDict:[request responseString]];
        [delegate onNetEngineResponse:tag entity:resultDic userInfo:nil];
    }
}


- (void)onGetCarSaleGPSListData:(ASIHTTPRequest *)request{
    
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary * resultDic = [BiCarSaleParser parserGetGpsListEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:resultDic userInfo:nil];
    }
}



- (void)onGetCarSaleCommonkData:(ASIHTTPRequest *)request{
    
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary * resultDic = [BiCarSaleParser parserCommonDict:[request responseString]];
        [delegate onNetEngineResponse:tag entity:resultDic userInfo:nil];
    }
}

@end
